package aaaaa;

import java.util.concurrent.DelayQueue;

public class MyDelayQueue {
    public static void main(String[] args) throws InterruptedException {

        DelayQueue<MyDelayOrder> delayQueue = new DelayQueue();
        // 4 秒后到期
        MyDelayOrder myDelayOrder = new MyDelayOrder("第一", 4000L + System.currentTimeMillis());
        // 1 秒后到期
        MyDelayOrder myDelayOrder1 = new MyDelayOrder("第二", 1000L + System.currentTimeMillis());
        // 10 秒后到期
        MyDelayOrder myDelayOrder2 = new MyDelayOrder("第三", 10000L + System.currentTimeMillis());

        delayQueue.offer(myDelayOrder);
        delayQueue.offer(myDelayOrder1);
        delayQueue.offer(myDelayOrder2);

        try {
//            Thread.sleep(7000);
        }catch (Exception e){
            e.printStackTrace();;
        }

        while (true) {
            // 死循环的方式， 一直从队列里获取到时间的元素
            MyDelayOrder result = delayQueue.take();
            System.out.println(result.getName()+System.currentTimeMillis());
        }
        // 输出结果为 第二 第一 第三
    }
}
